;; ---------- ---------- ---------- ---------- ---------- ---------- ----------
;;
;; NOTE: This will contain examples for:
;;       ~/.gnus
;;
;; ALSO: https://www.emacswiki.org/emacs/MultipleSMTPAccounts
;;       https://github.com/redguardtoo/mastering-emacs-in-one-year-guide/blob/master/gnus-guide-en.org
;;
;; ---------- ---------- ---------- ---------- ---------- ---------- ----------

;; List of email addresses and respective smtp:
;; (user-mail-address
;;  user-full-name
;;  message-send-mail-function/send-mail-function
;;  smtpmail-default-smtp-server/smtpmail-smtp-server
;;  smtpmail-smtp-service
;;  smtpmail-stream-type)
(setq my-email-addresses
      '(("foo@bar.glux" "foo" smtpmail-send-it "smtp.bar.glux" 465 ssl))

;; News servers
(setq gnus-select-method
      '(nntp "news.gnus.org"))

(setq gnus-secondary-select-methods
      '((nntp "news.gmane.org")
        (nntp "news.mozilla.org")))

;; General identity
(setq user-mail-address
      (nth 0 (nth 0 my-email-addresses))
      user-full-name
      (nth 1 (nth 0 my-email-addresses)))

;; Mail servers; just using gnus-secondary-select-methods.
;; (setq gnus-select-method
;;     '(nnimap "foo@bar.glux"))
(setq gnus-secondary-select-methods
      '((nnimap "foo.at.bar.glux"
         (nnimap-address "mail.bar.glux")
         (nnimap-server-port "imaps")
         (nnimap-stream ssl))
        (nnimap "foo.at.tanj.glux"
         (nnimap-address "imap.tanj.glux")
         (nnimap-server-port "imaps")
         (nnimap-stream ssl))
        (nnimap "foo.at.norf.glux"
         (nnimap-address "mailmail.norf.glux")
         (nnimap-server-port "imaps")
         (nnimap-stream ssl))))

;;NOTE: Update the message-mode settings to send with the correct account.
(setq my-email-addresses-index 0)
(defun smtpmail-loop-smtp-server ()
  (interactive)

  ;;restart index position
  (if (eq my-email-addresses-index (length my-email-addresses))
      (setq my-email-addresses-index 0) nil)

  ;;update email, name, and smtp infos
  (setq user-mail-address
        (nth 0 (nth my-email-addresses-index my-email-addresses))
        user-full-name
        (nth 1 (nth my-email-addresses-index my-email-addresses))
        message-send-mail-function
        (nth 2 (nth my-email-addresses-index my-email-addresses))
        send-mail-function
        (nth 2 (nth my-email-addresses-index my-email-addresses))
        smtpmail-default-smtp-server
        (nth 3 (nth my-email-addresses-index my-email-addresses))
        smtpmail-smtp-server
        (nth 3 (nth my-email-addresses-index my-email-addresses))
        smtpmail-smtp-service
        (nth 4 (nth my-email-addresses-index my-email-addresses))
        smtpmail-stream-type
        (nth 5 (nth my-email-addresses-index my-email-addresses)))
  (setq smtpmail-auth-credentials
        '((smtpmail-smtp-server smtpmail-smtp-service user-mail-address nil)))

  (setq current-from (concat "From: " user-full-name " <" user-mail-address ">"))

  ;;save position, replace the from field
  (setq message-article-current-point (point))
  (goto-char (point-min))
  (while (re-search-forward "^From:.*$" nil t)
     (replace-match current-from))
  (goto-char message-article-current-point)

  ;;advance index position, prompt
  (setq my-email-addresses-index (+ my-email-addresses-index 1))
  (message current-from))

(global-set-key (kbd "C-s-g") 'smtpmail-loop-smtp-server)
